package com.laicunba.event.listener;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.DangerBidEvent;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.AlertService;
import com.laicunba.service.FinancingTargetBidService;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.UserService;
import com.laicunba.util.ThreadUtil;

@Component
public class DangerBidEventListener implements ApplicationListener<DangerBidEvent> {
	private static final Logger LOG = Logger.getLogger(DangerBidEventListener.class);
	@Autowired
	private MobileMessageService mobileMessageService;
	@Autowired
	private AlertService alertService;
	@Autowired
	private FinancingTargetBidService financingTargetBidService;
	@Autowired
	private UserService userService;

	@Override
	public void onApplicationEvent(final DangerBidEvent event) {
		ThreadUtil.execute(new Runnable() {

			@Override
			public void run() {
				try {
					FinancingTargetBidDO bidDO = financingTargetBidService.get(FinancingTargetBidDO.class, event.getSource().toString());
					UserDO userDO = userService.get(UserDO.class, bidDO.getBidUserId());
					String alterMsg = String.format("用户[%s]存在危险绑卡交易事件,金额[%s]元,订单号[%s],主动防御掉单.", userDO.getMobile(), bidDO.getBuyAmount(), bidDO.getCertificateId());
					LOG.error(alterMsg);
					alertService.error(null, alterMsg);
					mobileMessageService.sendMsgAsync("18367156949", alterMsg);
					mobileMessageService.sendMsgAsync("18657102457", alterMsg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

}
